package leetCode;

import java.util.Objects;
import java.util.Stack;

public class T150 {
    public static void main(String[] args) {
        new SolutionT150().evalRPN(new String[]{"2","1","+","3","*"});
    }

}
class SolutionT150 {
    public int evalRPN(String[] tokens) {
        Stack<String>stack0=new Stack<>();

        for (String token:tokens){
            if (Objects.equals(token, "+")){
                String popB = stack0.pop();
                String popA = stack0.pop();
                Integer ret=Integer.parseInt(popA)+Integer.parseInt(popB);
                stack0.push(String.valueOf(ret));
            }else if (Objects.equals(token, "-")){
                String popB = stack0.pop();
                String popA = stack0.pop();
                Integer ret=Integer.parseInt(popA)-Integer.parseInt(popB);
                stack0.push(String.valueOf(ret));

            }else if (token.equals("*")){
                String popB = stack0.pop();
                String popA = stack0.pop();
                Integer ret=Integer.parseInt(popA)*Integer.parseInt(popB);
                stack0.push(String.valueOf(ret));
            }else if (token.equals("/")){
                String popB = stack0.pop();
                String popA = stack0.pop();
                Integer ret=Integer.parseInt(popA)/Integer.parseInt(popB);
                stack0.push(String.valueOf(ret));
            }else {
                stack0.push(token);
            }
        }
        return Integer.parseInt(stack0.pop());
    }
}
